package com.ruoyi.web.controller.set;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.constant.UserConstants;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.model.LoginUser;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.ServletUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.framework.web.service.TokenService;
import com.ruoyi.system.domain.SetStep;
import com.ruoyi.system.service.ISetStepService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 操作步骤Controller
 *
 * @author ruoyi
 * @date 2022-11-21
 */
@RestController
@Api(tags = "操作步骤信息")
@RequestMapping("/set/step")
public class SetStepController extends BaseController
{
    @Autowired
    private ISetStepService setStepService;

    @Autowired
    private TokenService tokenService;

    /**
     * 查询操作步骤列表
     */
    //@PreAuthorize("@ss.hasPermi('set:step:list')")
    @ApiOperation(value = "查询操作步骤列表")
    @GetMapping("/list")
    public TableDataInfo list(@ApiParam(value = "操作步骤参数", required = false) SetStep setStep)
    {
        startPage();
        List<SetStep> list = setStepService.selectSetStepList(setStep);
        return getDataTable(list);
    }

    /**
     * 查询操作步骤下拉接口
     */
    //@PreAuthorize("@ss.hasPermi('set:step:interface')")
    @ApiOperation(value = "查询操作步骤下拉接口")
    @GetMapping("/interface")
    public AjaxResult interfaces(@ApiParam(value = "操作步骤参数", required = false) SetStep setStep)
    {

        List<SetStep> list = setStepService.selectSetStepList(setStep);
        return AjaxResult.success(list);
    }

    /**
     * 导出操作步骤列表
     */
    //@PreAuthorize("@ss.hasPermi('set:step:export')")
    @Log(title = "操作步骤", businessType = BusinessType.EXPORT)
    @ApiOperation(value = "导出操作步骤列表")
    @GetMapping("/export")
    public AjaxResult export(HttpServletResponse response, @ApiParam(value = "操作步骤参数", required = false) SetStep setStep)
    {
        List<SetStep> list = setStepService.selectSetStepList(setStep);
        ExcelUtil<SetStep> util = new ExcelUtil<SetStep>(SetStep.class);
        return util.exportExcel( list, "操作步骤数据");
    }

    /**
     * 导入操作步骤列表
     */
    @Log(title = "操作步骤", businessType = BusinessType.IMPORT)
    //@PreAuthorize("@ss.hasPermi('set:step:import')")
    @ApiOperation(value = "导入操作步骤列表")
    @PostMapping("/importData")
    public AjaxResult importData(@ApiParam(value = "导入文件", required = true) MultipartFile file, boolean updateSupport) throws Exception
    {
        ExcelUtil<SetStep> util = new ExcelUtil<SetStep>(SetStep.class);
        List<SetStep> stepList = util.importExcel(file.getInputStream());
        LoginUser loginUser = tokenService.getLoginUser(ServletUtils.getRequest());
        String operName = loginUser.getUsername();
        String message = setStepService.importStep(stepList, updateSupport, operName);
        return AjaxResult.success(message);
    }

    /**
     * 导入模板
     */
    @ApiOperation(value = "导入模板")
    @GetMapping("/importTemplate")
    public AjaxResult importTemplate()
    {
        List<SetStep> list = setStepService.selectSetStepList(new SetStep());
        ExcelUtil<SetStep> util = new ExcelUtil<SetStep>(SetStep.class);
        return util.importTemplateExcel(list,"操作步骤数据");
    }

    /**
     * 获取操作步骤详细信息
     */
    //@PreAuthorize("@ss.hasPermi('set:step:query')")
    @ApiOperation(value = "获取操作步骤详细信息")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@ApiParam(value = "操作步骤id", required = true) @PathVariable("id") Long id)
    {
        return AjaxResult.success(setStepService.selectSetStepById(id));
    }

    /**
     * 新增操作步骤
     */
    //@PreAuthorize("@ss.hasPermi('set:step:add')")
    @Log(title = "操作步骤", businessType = BusinessType.INSERT)
    @ApiOperation(value = "新增操作步骤")
    @PostMapping
    public AjaxResult add(@ApiParam(value = "操作步骤实体类", required = true) @RequestBody SetStep setStep)
    {
        if (UserConstants.NOT_UNIQUE.equals(setStepService.checkNameUnique(setStep)))
        {
            return AjaxResult.error("新增操作步骤'" + setStep.getStepName() + "'失败，操作步骤名称已存在");
        }
        return toAjax(setStepService.insertSetStep(setStep));
    }

    /**
     * 修改操作步骤
     */
    //@PreAuthorize("@ss.hasPermi('set:step:edit')")
    @Log(title = "操作步骤", businessType = BusinessType.UPDATE)
    @ApiOperation(value = "修改操作步骤")
    @PutMapping
    public AjaxResult edit(@ApiParam(value = "操作步骤实体类", required = true) @RequestBody SetStep setStep)
    {
        if (UserConstants.NOT_UNIQUE.equals(setStepService.checkNameUnique(setStep)))
        {
            return AjaxResult.error("修改操作步骤'" + setStep.getStepName() + "'失败，操作步骤名称已存在");
        }
        return toAjax(setStepService.updateSetStep(setStep));
    }

    /**
     * 删除操作步骤
     */
    //@PreAuthorize("@ss.hasPermi('set:step:remove')")
    @Log(title = "操作步骤", businessType = BusinessType.DELETE)
    @ApiOperation(value = "删除操作步骤")
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@ApiParam(value = "操作步骤id数组", required = true) @PathVariable Long[] ids)
    {
        return toAjax(setStepService.deleteSetStepByIds(ids));
    }
}
